<?php
/**
 * +----------------------------------------------------------------------
 * | 九月科技-ztuc.cn
 * +----------------------------------------------------------------------
 *                      .::::.
 *                    .::::::::.            | AUTHOR: siyu
 *                    :::::::::::           | EMAIL: ztucke@ztuc.cn
 *                 ..:::::::::::'           | DATETIME: 2020/01/31
 *             '::::::::::::'
 *                .::::::::::
 *           '::::::::::::::..
 *                ..::::::::::::.
 *              ``::::::::::::::::
 *               ::::``:::::::::'        .:::.
 *              ::::'   ':::::'       .::::::::.
 *            .::::'      ::::     .:::::::'::::.
 *           .:::'       :::::  .:::::::::' ':::::.
 *          .::'        :::::.:::::::::'      ':::::.
 *         .::'         ::::::::::::::'         ``::::.
 *     ...:::           ::::::::::::'              ``::.
 *   ```` ':.          ':::::::::'                  ::::..
 *                      '.:::::'                    ':'````..
 * +----------------------------------------------------------------------
 */
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/3/10
 * Time: 20:36
 */

namespace app\lnadmin\controller;
use app\core\InitController;

use app\lnadmin\controller\Upload;
use think\facade\View;
use app\lnadmin\model\admin\Banner as BannerModel;
use app\lnadmin\model\admin\Image as ImageModel;

/**
 * 轮播图
 * Class Banner
 * @package app\controller\admin
 */
class Banner extends InitController
{
    /**
     * 列表
     * @return string
     * @throws \Exception
     * @author: MK
     * @Time: 2020/4/5 12:32
     */
    public function list()
    {
        return View::fetch('banner');
    }

      //列表详情
    public function index_json($limit='15')
    { 
        $data = BannerModel::order('id desc')->paginate($limit);
      if (!empty($data)){
            // 获取图片
            foreach ($data as $k=>$v){
                $data[$k]['image'] = ImageModel::image($v['id'],Upload::TYPE_BANNER);
            }
        }
        $this->result($data);
    }
   
    /**
     * 新增商户
     * @return mixed
     */
  public function add()
    {     
        if ($this->request->isPost()) {
            $param = $this->request->param();
            
        if (!$param['desc']) return false;
               BannerModel::add($param);
            add_admin_log('add', '轮播图片');// 记录行为日志
         return $this->success(true, 0, '新增成功');
            } 
        return View::fetch('saveBanner');
    }
    /**
     * 编辑商户
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
        public function edit()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            
            $edit = BannerModel::edit($param); 
            add_admin_log('edit', '轮播图片');// 记录行为日志
        return $edit ? $this->saveSuccess(true, 0, '修改成功') : $this->error('修改失败');
          
        }
         return $this->fetch('saveBanner', [
            'model' => ImageModel::where(['table_id' => input('id'), 'types' => upload::TYPE_BANNER])->find(), 
            'data'  => BannerModel::where('id',input('id'))->find()
        ]);
    }

    /**
     * 删除商户
     */
    public function del()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            BannerModel::destroy($param['id']);
            add_admin_log('del', '轮播图片');// 记录行为日志
            $this->success('删除成功');
        }
    }

}